package process;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.widget.ProgressBar;

public class SendAllFile {

	
	// khue has mod here 
	
	public int  SendInfoStartSend(String host, int port,String InfoStartSend) 
	{
		int flag = 0;
		try {
			
			Socket socket = new Socket(host, port);
			OutputStream os= socket.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(InfoStartSend);
			bw.flush();
			bw.close();
			os.close();
			socket.close();
			flag = 1;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	public int sendfilename(String host, int port,String filename) 
	{
		
		int flag = 0;
		try {
			
			Socket socket = new Socket(host, port);
			OutputStream os= socket.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(filename);
			bw.flush();
			bw.close();
			os.close();
			socket.close();
			flag = 1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
		
		
		
	}
	public int sendInfoEndSend(String host, int port,String InfoEndSend) 
	{
		int flag = 0;
		try {
			
			Socket socket = new Socket(host, port);
			OutputStream os= socket.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(InfoEndSend);
			bw.flush();
			bw.close();
			os.close();
			socket.close();
			flag = 0;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	/*public int SenContentFile(String Path, String myfile,String host, int port  )
	{
		int kt =0 ;
		int flag = 0;
		String myPath = Path +"/" +myfile;
		FileInputStream fis = null;
		int size = 1400;
		File file = new File(myPath);
		try 
		{
			fis = new FileInputStream(file);
		} 
		catch (FileNotFoundException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(file.exists())
		{
			try {
				while(flag!=-1)
				{
					InetAddress IA = InetAddress.getByName(host);
					Socket socket = new Socket(IA, port);
					byte[]data = new byte[size];
					flag = fis.read(data, 0, data.length);
					if(flag ==-1 || flag<size)
					{
						OutputStream out = socket.getOutputStream();
						out.write(data, 0, flag);
						out.flush();
						flag = -1;
					}
					else
					{
						OutputStream out = socket.getOutputStream();
						out.write(data, 0, data.length);
						out.flush();
						
					}
					socket.close();
					
				}
				
				kt = 1;
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				kt = 0;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				kt = 0;
			}
			
		}
		return kt;
	}*/
	ProgressBar pb ;
	private int ktt;
	public int SenContentFile(ProgressBar pBs, String Paths,String myfiles,String hosts, int ports  )
	{
		ktt=1;
		final ProgressBar pb = pBs;
		final String Path= Paths;
		final String myfile = myfiles;
		final String host = hosts;
		final int port = ports;  
		
		final Thread myThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
			
				pb.setMax(100);
				String myPath = Path +"/" +myfile;
				File file = new File(myPath);
					int flag = 0;
					FileInputStream fis = null;
					int size = 1400;
					try 
					{
						fis = new FileInputStream(file);
					} 
					catch (FileNotFoundException e1) 
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					if(file.exists())
					{
						try {
							
							int n = 0;// số lần lặp
							if(file.length()*1.0/size > (int)(file.length()/size))
								n = (int)(file.length()/size) + 1;
							else
								n = (int)(file.length()/size);
							int k = 0;// k là số lần lặp để progressbar tăng thêm 1%
							if(n/100 < 1)
								k = 1;
							else
								k = (int)(n/100);
							int u = 1;
							int p = 1;
							while(flag!=-1)
							{
								InetAddress IA = InetAddress.getByName(host);
								Socket socket = new Socket(IA, port);
								byte[]data = new byte[size];
								flag = fis.read(data, 0, data.length);	
								
								if(u == k)
								{
									pb.setProgress(p);
									p++;
									u = 0;
								}
								u++;
								
								if(flag ==-1 || flag<size)
								{
									OutputStream out = socket.getOutputStream();
									out.write(data, 0, flag);
									out.flush();
									pb.setProgress(100);
									//pb.setVisibility(ProgressBar.GONE);
									flag = -1;
									
								}
								else
								{
									OutputStream out = socket.getOutputStream();
									out.write(data, 0, data.length);
									out.flush();
									
								}
								socket.close();
								
							}
							
			
						ktt = 1;
						} catch (UnknownHostException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							ktt = 0;
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							ktt = 0;
						}
						
					}
				//}
				
			}
		});
		myThread.start();
		return ktt;
	}
	public int  SendInfoStartSendUDP(String host, int port,String InfoStartSend) 
	{
		int kt = 0;
		DatagramSocket socket = null;
		DatagramPacket data ;
		try {

			InetAddress IA = InetAddress.getByName(host);
			data = new DatagramPacket(InfoStartSend.getBytes(), InfoStartSend.length(), IA, port);
			socket = new DatagramSocket();
			socket.send(data);
			kt = 1;
			
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		socket.close();
		return kt;
	}
	public int SendfilenameUDP(String host, int port,String filename)
	{
		int kt = 0;
		DatagramSocket socket = null;
		DatagramPacket data ;
		try {

			InetAddress IA = InetAddress.getByName(host);
			data = new DatagramPacket(filename.getBytes(), filename.length(), IA, port);
			socket = new DatagramSocket();
			socket.send(data);
			kt = 1;
			socket.close();
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return kt;
	}
	public int SenContentFileUDP(String Path, String myfile,String host, int port  )
	{
		int kt =0 ;
		
		int flag = 0;
		String myPath = Path +"/" +myfile;
		FileInputStream fis = null;
		int size = 1400;
		File file = new File(myPath);
		int dem =0;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			while(flag!=-1){
				DatagramSocket socket = null;
				DatagramPacket data ;
				InetAddress IA = InetAddress.getByName(host);
				byte[]data1 = new byte[size];
				flag = fis.read(data1, 0, data1.length);
				if(flag ==-1 || flag<size)
				{
					data = new DatagramPacket(data1, flag, IA, port);
					socket = new DatagramSocket();
					socket.send(data);
					dem ++;
					flag = -1;
				}
				else
				{
					
					data = new DatagramPacket(data1, data1.length, IA, port);
					socket = new DatagramSocket();
					socket.send(data);
					dem++;
				}
				socket.close();
			}
			
			//dem =1;
			//socket.close();
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
		return dem;
	}
}
